package portal.conf;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.HostEntity;
import portal.conf.entity.HostGroupEntity;
@Component
@Scope("singleton")
public class HostGroupConf extends HostsConf{
	public HostGroupConf(){
		if(IsDebug){
			setEndTitle("End HostGroup");
			setStartTitle("Begin HostGroup");
		}
	}	
	@Value("${conf.hostgroupfile.end}")  
	public void setEndTitle(String fname) {  
		super.setEndTitle(fname);  
	}
	@Value("${conf.hostgroupfile.start}")   
	public void setStartTitle(String fname) {  
		super.setStartTitle(fname);;  
	}
	@Override
	public boolean removeConf(String hostName){
		if(MyStringUtils.isEmpty(hostName))
			return false;
		removeConf(findConfByName(hostName));
		return true;		
	}
	@Override
	public boolean  removeConf(BaseConf bConf){
		if(bConf==null)return false;
		if(super.removeConf(bConf)){
			selectAllConf(new File(fileName), HostGroupEntity.class);//修改完成后更改
			return true;
		}
		return false;
		//System.out.println(getLastModified());
	}
	@Override
	public boolean insertConf(String hostinfo){
		return insertConf((new HostGroupEntity()).setValue(hostinfo));
	}
	@Override
	public boolean insertConf(BaseConf bConf){
		if(isEntityExist(bConf))return false;
		if(confList==null ||confList.isEmpty())return false;
		BaseConf b=(BaseConf)confList.get(confList.size()-1);//.getStart
		bConf.setLineStart(b.getLineStart());
		bConf.setLineEnd(bConf.getLineStart());
		super.updateFile(bConf, fileName, 1);
		super.selectAllConf(new File(fileName),HostGroupEntity.class );//selectAllConf();
		//System.out.println(getLastModified());
		return true;
	}
	public boolean updateConf(String groupName ,String[] node ){
		HostGroupEntity bConf=(HostGroupEntity)findConfByName(groupName);
		if(bConf!=null){
			if(node==null || node.length==0)
				return false;
			bConf.addHost(node);
			updateFile(bConf, fileName, CONF_REPLACE_COMMENT);
			super.selectAllConf(new File(fileName),HostGroupEntity.class );
			return true;
		}else			
			return false;
	}
	/*public boolean updateConf(BaseConf hge){
		//if(isEntityExist(hge)){
		BaseConf bConf=findConfByName(hge.getKey());
		if(bConf!=null){
			hge.setLineEnd(bConf.getLineEnd());
			hge.setLineStart(bConf.getLineStart());
			updateFile(bConf, fileName, 3);
		}else
			insertConf(hge);
		return true;
	}*/
	public List<BaseConf> selectAllConf(){
		return super.selectAllConf(fileName, HostGroupEntity.class);
	}
	public static void main(String[] args){
		HostGroupConf queueConf=new HostGroupConf();
		List<BaseConf> list=queueConf.selectAllConf();
		HostGroupEntity qe=new HostGroupEntity("group00   (1  2  3  4 5)");
		//qe.setQUEUE_NAME("xuwenyuantest");
		//queueConf.insertConf("//app//unischeduler//queues.conf",qe);
		//queueConf.removeConf("test");
		//queueConf.re 
///秒级别无异常，毫秒级别不能及时更新
		//queueConf.insertConf(qe);
		//queueConf.removeConf("group00");
		//queueConf.updateConf("group001", "66 77".split(" "));
		System.out.println(list.size());
		list=queueConf.selectAllConf();
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}
}
